package com.biwano.swapmyjob.beans.user;

import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import com.biwano.swapmyjob.beans.BeanException;
import com.biwano.swapmyjob.beans.ValidatorBean;
import com.biwano.swapmyjob.dao.DBException;


@RequestScoped
@ManagedBean
public class LoginBean extends ValidatorBean {
	
	
	private String login;
	private String password;
	
	@ManagedProperty(value="#{userBean}")
	private UserBean userBean;
	
	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}
	


	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public void validateInput() {
		checkMandatory(login,"login");
		checkMandatory(password,"password");
	}
	@Override
	public RETURNVALUE beanAction() throws BeanException, DBException {
		userBean.authentify(getLogin(), getPassword());
		return RETURNVALUE.gotohome;
	}	
	@Override
	public void onBeanActionError()
	{
		addError("login", "identitywrong");
	}
	public String logoff()
	{
		userBean.setAuthenticated(false);
		userBean.setUser(null);
		return RETURNVALUE.success.toString();
	}
	@Override
	public String getFormId() {
		return "login";
	}
}
